﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.iRods.Common;

namespace Renci.iRods.Data
{
    public class User : MetaDataItem
    {
        [iRodsColumn(DataObjectAttributes.COL_USER_ID)]
        public long Id { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_NAME)]
        public string Name { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_TYPE)]
        public string Type { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_ZONE)]
        public string Zone { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_INFO)]
        public string Info { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_COMMENT)]
        public string Comment { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_CREATE_TIME)]
        public DateTime CreatedTime { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_USER_MODIFY_TIME)]
        public DateTime ModifiedTime { get; internal set; }

        protected override string MetadataFullName
        {
            get { return this.Name; }
        }

        protected override IRodsQuery MetadataQuery
        {
            get
            {
                var query = new IRodsQuery();
                query.AddOutputColumn(DataObjectAttributes.COL_META_USER_ATTR_ID);
                query.AddOutputColumn(DataObjectAttributes.COL_META_USER_ATTR_NAME);
                query.AddOutputColumn(DataObjectAttributes.COL_META_USER_ATTR_VALUE);
                query.AddOutputColumn(DataObjectAttributes.COL_META_USER_ATTR_UNITS);
                query.AddCondition(DataObjectAttributes.COL_USER_NAME, string.Format("='{0}'", this.Name));

                return query;
            }
        }

        protected override Func<IRodsDataReader, MetaData> MetadataProjector
        {
            get
            {
                return (r) =>
                {
                    return new MetaData(
                        r.GetInt32(DataObjectAttributes.COL_META_USER_ATTR_ID), 
                        r.GetString(DataObjectAttributes.COL_META_USER_ATTR_NAME),
                        r.GetString(DataObjectAttributes.COL_META_USER_ATTR_VALUE),
                        r.GetString(DataObjectAttributes.COL_META_USER_ATTR_UNITS));
                };
            }
        }

        internal User(IRodsConnection connection)
            : base(connection, "-u")
        {

        }

        public User(IRodsConnection connection, string name)
            : base(connection, "-u")
        {
            this.Name = name;
        }

        public override void Refresh()
        {
            this.Exists = this.Load((q) =>
            {
                q.AddCondition(DataObjectAttributes.COL_USER_NAME, string.Format("='{0}'", this.Name));
            });
        }

    }
}
